
package com.tools.cleanmaster.ui.card;

import android.app.Activity;
import android.support.annotation.NonNull;

import com.tools.ad.EntranceType;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.ui.card.factory.CardDataHelper;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.card.viewholder.ACardViewHolder;

/**
 * 展示卡片基类
 */
public abstract class ACardItem implements Comparable<ACardItem> {

    private final static String TAG = CardDataHelper.TAG;

    /**
     * 应用级别的Context
     */
    protected final CleanApplication imContext;

    protected EntranceType mEntranceType;

    protected boolean isReportShow = true;

    private int mPos;

    public ACardItem(EntranceType entranceType, int pos) {
        this.mPos = pos;
        this.mEntranceType = entranceType;
        this.imContext = CleanApplication.getInstance();
    }

    /**
     * 返回卡片展示的UI样式类型
     */
    public abstract CardViewType getCardViewType();

    /**
     * 判断卡片是否可以展示，各卡片自行控制
     */
    public abstract boolean needShow(EntranceType entranceType);

    /**
     * 等同于Adapter的getView,具体卡片控制UI展示,子类卡片需要在每次展示上报时需要主动调用super.bindCardView
     *
     * @param activity    卡片依附的activity
     * @param viewHolder  承载view的holder
     * @param cardAdapter adapter用来控制操作list
     */
    public void bindCardView(Activity activity, ACardViewHolder viewHolder,
                             CardListAdapter cardAdapter, int position) {
    }

    /**
     * 获取卡片名字
     */
    public abstract String getCardName();

    /**
     * 卡片依附的界面onDestroy回调，主要处理一下销毁动作
     */
    public void onActivityDestroy() {
    }

    /**
     * 卡片依附的界面onResume回调,子类卡片需要在每次展示上报时需要主动调用super.onActivityResume
     */
    public void onActivityResume() {
    }

    public void setPosition(int pos) {
        mPos = pos;
    }

    public int getPosition() {
        return mPos;
    }

    @Override
    public int compareTo(@NonNull ACardItem another) {
        if (mPos < another.mPos) {
            return -1;
        } else if (mPos > another.mPos) {
            return 1;
        } else if (mPos == another.mPos) {
            if (this instanceof AdCardItem) {
                return -1;
            } else if (another instanceof AdCardItem) {
                return 1;
            } else if (this instanceof LoadingAdCardItem) {
                return -1;
            }
        }
        return 0;
    }

    public static ACardItem createCardItem(String card, int pos, EntranceType entranceType) {
        if (CardType.RATE.key.equals(card)) {
            return new RateItem(entranceType, pos);
        } else if (CardType.TRASH.key.equals(card)) {
            return new TrashCardItem(entranceType, pos);
        } else if (CardType.PHONE_MEDIA.key.equals(card)) {
            return new PhoneStateCard(entranceType, pos);
        } else if (CardType.CHARGING.key.equals(card)) {
            return new ChargingCardItem(entranceType, pos);
        } else if (CardType.AUTO_CLEAN.key.equals(card)) {
            return new AutoCleanCardItem(entranceType, pos);
        } else if (CardType.SWIPE.key.equals(card)) {
            return new SwipeCardItem(entranceType, pos);
        } else if (CardType.POWER.key.equals(card)) {
            return new FuncPowerItem(entranceType, pos);
        } else if (CardType.COOLER.key.equals(card)) {
            return new FuncCoolerItem(entranceType, pos);
        } else if (CardType.BOOSTER.key.equals(card)) {
            return new FuncBoosterItem(entranceType, pos);
        } else if (CardType.AD.key.equals(card)) {
            return new AdCardItem(entranceType, pos);
        } else if (CardType.LOADING_AD.key.equals(card)) {
            return new LoadingAdCardItem(entranceType, pos);
        }

        return null;
    }
}
